package com.changeover.gwt.client.view.impl;

import com.changeover.gwt.client.view.ILoginView;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.dom.client.HasFocusHandlers;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class LoginView extends Composite implements ILoginView {
    private final Button loginButton;
    private final TextBox loginField;
    private final PasswordTextBox passwordField;
    private final Label errorLabel;

    public LoginView() {
        DecoratorPanel loginView = new DecoratorPanel();
        loginView.setStylePrimaryName("login-view");

        VerticalPanel loginViewContainer = new VerticalPanel();
        loginViewContainer.setStylePrimaryName("login-view-container");
        loginView.add(loginViewContainer);
        
        VerticalPanel loginForm = new VerticalPanel();
        loginForm.setStylePrimaryName("form-login");
        loginViewContainer.add(loginForm);
        
        HorizontalPanel headerPanel = new HorizontalPanel();
        headerPanel.setStylePrimaryName("header");
        Label headerLabel = new Label("Login");
        headerPanel.add(headerLabel);
        loginForm.add(headerPanel);

        HorizontalPanel loginPanel = new HorizontalPanel();
        loginPanel.setSpacing(10);
        loginPanel.setStylePrimaryName("form-el-container");

        Label usernameLabel = new Label("Username:");
        usernameLabel.setStylePrimaryName("form-el-label");
        loginField = new TextBox();
        loginPanel.add(usernameLabel);
        loginPanel.add(loginField);
        loginForm.add(loginPanel);

        HorizontalPanel passwordPanel = new HorizontalPanel();
        passwordPanel.setSpacing(10);
        passwordPanel.setStylePrimaryName("form-el-container");

        Label passwordLabel = new Label("Password:");
        passwordLabel.setStylePrimaryName("form-el-label");
        passwordField = new PasswordTextBox();
        passwordPanel.add(passwordLabel);
        passwordPanel.add(passwordField);
        loginForm.add(passwordPanel);

        HorizontalPanel buttonsPanel = new HorizontalPanel();
        buttonsPanel.setSpacing(10);
        buttonsPanel.setStylePrimaryName("form-buttons-container");

        loginButton = new Button("Login Button");
        loginButton.setText("Login");
        loginButton.setStylePrimaryName("form-button");
        buttonsPanel.add(loginButton);
        loginForm.add(buttonsPanel);
        
        errorLabel = new Label("");
        errorLabel.setStylePrimaryName("form-el-container msg");
        loginForm.add(errorLabel);
        
        loginViewContainer.add(getAppHint());

        initWidget(loginView);
    }
    
    private HTML getAppHint() {
        HTML html = new HTML("Hello, here is a GWT version of ChangeOver app.<br/>Login by <strong>agent/123456</strong> in order to reach Tasks screen.");
        html.setStylePrimaryName("app-info");
        return html;
    }

    @Override
    public HasClickHandlers getLoginButton() {
        return loginButton;
    }

    @Override
    public String getLoginText() {
        return loginField.getText();
    }

    @Override
    public String getPasswordText() {
        return passwordField.getText();
    }

    @Override
    public String getErrorText() {
        return errorLabel.getText();
    }

    @Override
    public void setErrorText(String errorMessage) {
        errorLabel.setText(errorMessage);
    }

    @Override
    public Widget asWidget() {
        return this;
    }

    @Override
    public void clean() {
        loginField.setText("");
        passwordField.setText("");
    }

    @Override
    public HasFocusHandlers getLoginField() {
        return loginField;
    }

    @Override
    public HasFocusHandlers getPasswordField() {
        return passwordField;
    }
}
